1 00:00:00,490 --> 00:00:01,090 Hey there. 2 00:00:01,090 --> 00:00:01,960 Welcome back. 3 00:00:01,960 --> 00:00:07,570 Now that we have our guns completed, we need to address the next issue in our game, which is the fact 4 00:00:07,570 --> 00:00:10,720 that our guns have a finite amount of ammo. 5 00:00:10,750 --> 00:00:15,790 This means we need a script, a system where players can grab additional reserve ammo for their guns 6 00:00:15,790 --> 00:00:16,930 when they run out. 7 00:00:16,960 --> 00:00:20,800 That's why I have added these ammo crates around the map. 8 00:00:20,800 --> 00:00:22,930 They're very simple models. 9 00:00:22,930 --> 00:00:28,750 If we go ahead and take a look that just have a proximity prompt and a billboard guy inside of them, 10 00:00:28,750 --> 00:00:35,980 and the plan is to listen for when this proximity prompt is triggered and then give ammo to the gun 11 00:00:35,980 --> 00:00:38,320 our player currently has equipped. 12 00:00:38,930 --> 00:00:45,500 There are three attributes in our ammo boxes which signify how much ammo is currently in the ammo box, 13 00:00:45,500 --> 00:00:51,140 what's the maximum amount of ammo it can hold, and how long does it take to refill the ammo box once 14 00:00:51,140 --> 00:00:52,160 it runs out? 15 00:00:52,490 --> 00:00:56,420 Each of these ammo boxes are tagged with a string called Ammo Box. 16 00:00:56,420 --> 00:01:02,720 If your ammo boxes are not tagged with the string, make sure to create a new tag and add that tag to 17 00:01:02,720 --> 00:01:03,530 them all. 18 00:01:03,680 --> 00:01:09,050 Otherwise, we can go ahead and create a new server script inside of our server script service. 19 00:01:09,050 --> 00:01:15,470 We'll call this our ammo box handlers, and we can get started scripting the functionality for our ammo 20 00:01:15,470 --> 00:01:16,310 boxes. 21 00:01:17,180 --> 00:01:19,820 We'll be using the same template that we've always been using. 22 00:01:19,820 --> 00:01:22,460 I'll just delete this section and delete this section. 23 00:01:22,460 --> 00:01:28,550 And then we can go ahead and get the collection service in order to grab all of the ammo boxes in our 24 00:01:28,550 --> 00:01:29,120 game. 25 00:01:32,030 --> 00:01:36,020 And what we can go ahead and do is we can create a variable called ammo boxes. 26 00:01:36,260 --> 00:01:38,870 And that will be equal to the collection service. 27 00:01:38,870 --> 00:01:45,830 And we want to get tagged all of the ammo boxes or the instances that have the ammo box tag. 28 00:01:46,490 --> 00:01:51,890 Then we would go ahead and loop through every single ammo box inside of our ammo boxes table. 29 00:01:52,490 --> 00:01:56,390 And here we would listen for when the ammo box has its proximity prompt triggered. 30 00:01:56,420 --> 00:02:01,910 We'd also want to listen for when the attributes on our ammo boxes changed, so that way we can update 31 00:02:01,910 --> 00:02:06,890 the information being displayed in the billboard guys for our ammo boxes. 32 00:02:06,920 --> 00:02:09,860 So let's go ahead and create a couple functions. 33 00:02:09,860 --> 00:02:14,390 One is going to be to listen for when the proximity prompt is triggered. 34 00:02:14,390 --> 00:02:16,280 So we'll call this on triggered. 35 00:02:16,580 --> 00:02:19,400 And we're going to get past the player that triggered it. 36 00:02:19,400 --> 00:02:24,140 And then we also want to know which ammo box had the proximity prompt triggered. 37 00:02:24,140 --> 00:02:29,270 We also want to listen for when an attribute is changed on our ammo boxes. 38 00:02:29,270 --> 00:02:32,120 We'll call it on attribute changed. 39 00:02:32,120 --> 00:02:35,600 And again we want to know which ammo box had its attribute changed. 40 00:02:35,600 --> 00:02:41,180 So we'll pass ammo box as well as the name of what attribute changed on our ammo box. 41 00:02:42,100 --> 00:02:47,290 Then we can go ahead and refer to our ammo box to get the attachment that is inside of each one of them. 42 00:02:47,290 --> 00:02:51,670 Get the proximity prompt and we want to listen to that proximity prompt triggered event. 43 00:02:52,280 --> 00:02:54,620 And we'll connect a function, we'll get that player. 44 00:02:54,620 --> 00:02:57,980 And here we will call our on triggered function. 45 00:02:57,980 --> 00:03:02,750 We will pass the player as well as the box that got triggered. 46 00:03:02,750 --> 00:03:06,020 We also want to listen to the attribute changed event. 47 00:03:06,020 --> 00:03:07,520 For our ammo box. 48 00:03:07,520 --> 00:03:11,690 We'll connect get the function and the name of the attribute that changed. 49 00:03:11,690 --> 00:03:14,450 And then we'll call our on attribute change function. 50 00:03:14,450 --> 00:03:18,830 Pass our ammo box and pass the name of the attribute that changed. 51 00:03:19,450 --> 00:03:24,880 And then lastly, we want to be able to update the text inside of our billboard GIS to display how much 52 00:03:24,880 --> 00:03:28,030 ammo is left inside of our ammo box. 53 00:03:28,030 --> 00:03:30,130 So our billboard GIS are very simple. 54 00:03:30,130 --> 00:03:35,800 It's just a text label inside of them that just says Ammo left and will update some text inside of them. 55 00:03:35,800 --> 00:03:43,570 So since all of the text inside of our ammo boxes are blank, that means we want to go ahead and go 56 00:03:43,570 --> 00:03:47,860 through every single ammo box and update their text to display how much ammo they have left. 57 00:03:47,860 --> 00:03:54,610 So we'll refer to our box, get the attachment, get the billboard guy inside of them, get the text 58 00:03:54,610 --> 00:03:59,350 label and update the text to something like we could say ammo left. 59 00:03:59,980 --> 00:04:07,270 And then we're going to concatenate this and refer to the box and get the attribute of current ammo. 60 00:04:08,800 --> 00:04:11,260 Now inside of the on triggered function. 61 00:04:11,260 --> 00:04:17,950 What we want to go ahead and do is we want to check to see if the player that triggered this proximity 62 00:04:17,950 --> 00:04:21,730 prompt actually has a gun equipped or a two on them. 63 00:04:21,730 --> 00:04:23,680 So we'll create a variable called gun. 64 00:04:23,680 --> 00:04:26,440 And we're going to refer to the player's character. 65 00:04:27,250 --> 00:04:31,720 We're going to find first child, which is a tool. 66 00:04:32,410 --> 00:04:35,980 If they do not have a tool equipped, then we're just going to return. 67 00:04:36,130 --> 00:04:46,030 If this tool is not a gun, so if not gun get attribute gun type, then we're also going to return. 68 00:04:46,720 --> 00:04:53,020 Otherwise what we can go ahead and do is we can go ahead and get the properties for our gun. 69 00:04:53,410 --> 00:04:58,330 And that's going to be equal to require gun dot properties. 70 00:04:58,330 --> 00:05:04,120 And this is because we want to check if the reserve ammo that's currently in the gun is not equal to 71 00:05:04,120 --> 00:05:07,660 the maximum reserve ammo that our guns can hold. 72 00:05:07,660 --> 00:05:14,230 So if gun get attribute ammo reserve, if that is equal to the properties. 73 00:05:14,230 --> 00:05:21,190 And the property I believe is called Max reserve ammo, then we don't need to give the player any additional 74 00:05:21,190 --> 00:05:24,910 reserve ammo because they're already full, so we'll just return. 75 00:05:26,540 --> 00:05:33,140 Otherwise, we can go ahead and calculate how much ammo they need to fill up their ammo reserve. 76 00:05:33,140 --> 00:05:34,730 So we'll call it ammo needed. 77 00:05:34,730 --> 00:05:36,320 And that's going to be equal to. 78 00:05:36,320 --> 00:05:38,780 And we're going to use the math dot minimum function. 79 00:05:38,780 --> 00:05:42,530 And what we're going to do is we're going to get properties dot max reserve ammo. 80 00:05:42,530 --> 00:05:46,370 And then we're going to subtract that by their current ammo reserve. 81 00:05:46,370 --> 00:05:49,100 So gun get attribute ammo reserve. 82 00:05:49,980 --> 00:05:56,520 And just in case they need more ammo than what is inside of our ammo box, the second value we're going 83 00:05:56,520 --> 00:05:58,260 to pass here is ammo box. 84 00:05:58,920 --> 00:06:00,600 Get attributes. 85 00:06:01,590 --> 00:06:08,370 Current ammo, so we do not go above how much ammo is currently inside of our ammo box. 86 00:06:08,370 --> 00:06:13,680 So once we've calculated how much ammo they need, then we can go ahead and update the attribute on 87 00:06:13,680 --> 00:06:15,690 this gun set attribute. 88 00:06:17,100 --> 00:06:23,760 Ammo reserve and we want to set it equal to gun get attribute ammo reserve. 89 00:06:24,270 --> 00:06:27,570 And then we also want to add the ammo they need. 90 00:06:27,990 --> 00:06:32,550 And then inside of our ammo box we want to go ahead and subtract the ammo they needed. 91 00:06:32,550 --> 00:06:37,890 So ammo box set attribute current ammo. 92 00:06:38,450 --> 00:06:41,240 And it's going to be equal to ammo box. 93 00:06:42,200 --> 00:06:48,620 Get attribute current ammo and then subtract that by how much ammo we need. 94 00:06:49,850 --> 00:06:54,470 And then from this point, this is where we would want to go ahead and play a sound, to make it sound 95 00:06:54,470 --> 00:06:57,890 like the player reloaded their weapon with extra ammo. 96 00:06:57,890 --> 00:07:03,950 So instead of replicated storage inside of shared and then inside of audio, I have a sound here called 97 00:07:03,950 --> 00:07:04,580 Collect Ammo. 98 00:07:04,610 --> 00:07:05,690 I'll play it for you. 99 00:07:06,350 --> 00:07:09,320 So we want to play that sound when the player grabs ammo. 100 00:07:09,840 --> 00:07:12,090 So we can go ahead and create a reference to this sound. 101 00:07:12,090 --> 00:07:14,010 I'll just call it the Re-fill sound. 102 00:07:14,010 --> 00:07:16,770 And that means we're going to have to grab replicated storage. 103 00:07:20,220 --> 00:07:25,770 And then that's going to be equal to replicated storage dot shared dot audio dot collect ammo. 104 00:07:25,770 --> 00:07:28,500 And then we can go ahead and clone this sound. 105 00:07:28,500 --> 00:07:30,090 So clone is going to be equal. 106 00:07:30,090 --> 00:07:36,240 To refill sound clone we'll set the parent to be equal to the ammo box. 107 00:07:37,790 --> 00:07:39,650 And then we can go ahead and play that sound. 108 00:07:39,650 --> 00:07:44,930 And then when that sound has ended, we'll go ahead and destroy it. 109 00:07:45,780 --> 00:07:46,530 Cool. 110 00:07:46,530 --> 00:07:52,140 Now, what we need to go ahead and do is we need to listen for when an attribute changes inside of our 111 00:07:52,140 --> 00:07:52,770 ammo box. 112 00:07:52,770 --> 00:07:56,670 In this case, the one we want to listen to is going to be the current ammo attribute. 113 00:07:57,300 --> 00:08:01,740 So if the name of the attribute that changed is equal to current ammo. 114 00:08:02,660 --> 00:08:04,100 Then what we want to do. 115 00:08:04,640 --> 00:08:09,980 Is we want to go ahead and update the text label inside of our ammo box. 116 00:08:09,980 --> 00:08:14,960 So we'll just copy this code down here, paste it right there, and then let me update this with ammo 117 00:08:14,960 --> 00:08:15,950 box. 118 00:08:15,950 --> 00:08:18,050 And then I'll update it right here as well. 119 00:08:19,950 --> 00:08:26,460 And then we want to check if the current ammo that's inside of our ammo box is equal to zero. 120 00:08:26,460 --> 00:08:31,980 If it is, then that means we need to go ahead and refill the ammo box. 121 00:08:31,980 --> 00:08:41,580 So if ammo box get attribute current ammo, if that is equal to zero, then what we're going to do is 122 00:08:41,580 --> 00:08:45,630 we're going to disable the proximity prompt inside of the ammo box. 123 00:08:45,630 --> 00:08:47,580 So players can't interact with it anymore. 124 00:08:47,580 --> 00:08:52,710 So ammo box, dot attachment dot proximity prompt dot enable. 125 00:08:52,710 --> 00:08:54,390 We're going to set that to false. 126 00:08:55,340 --> 00:08:59,360 And then here's where we're going to wait for that refill cooldown. 127 00:08:59,360 --> 00:09:06,140 So we'll get our ammo box, get attributes, refill cooldown, and then we're going to wait for that 128 00:09:06,140 --> 00:09:06,860 duration. 129 00:09:06,860 --> 00:09:13,610 And then after that duration is up, we can go ahead and set on our ammo box set attribute. 130 00:09:14,690 --> 00:09:19,070 Current ammo, and that's going to be equal to the ammo box. 131 00:09:19,730 --> 00:09:22,220 Get attribute Max ammo. 132 00:09:23,450 --> 00:09:28,340 And then once that's done, we can go ahead and set the proximity prompt to be enabled again. 133 00:09:30,280 --> 00:09:35,320 Now, another thing we can do just in case, is inside of our on triggered function. 134 00:09:35,320 --> 00:09:40,660 We can go ahead and check to see and make sure that we actually have enough ammo inside of our ammo 135 00:09:40,660 --> 00:09:41,470 box. 136 00:09:41,470 --> 00:09:51,520 So what we can go ahead and do is we can check at the top if our ammo box get attribute current ammo. 137 00:09:52,210 --> 00:09:57,490 If that's equal to zero, then we're just going to return as well because we have no ammo to give this 138 00:09:57,490 --> 00:09:58,090 player. 139 00:09:58,090 --> 00:10:04,660 This is just in case because this function shouldn't execute since we're going to be disabling the proximity 140 00:10:04,660 --> 00:10:05,050 prompt. 141 00:10:05,050 --> 00:10:08,980 But as I said, just in case, we're going to put this in that function as well. 142 00:10:09,830 --> 00:10:14,480 And now I believe that's all we need to do to set up our ammo boxes. 143 00:10:14,480 --> 00:10:20,660 So to go ahead and test this out, I'll go into my server storage, grab one of my guns, copy that 144 00:10:20,660 --> 00:10:22,700 and paste it into my starter pack. 145 00:10:22,700 --> 00:10:24,620 And then we can go and play test the game. 146 00:10:25,650 --> 00:10:27,690 No errors in the console, which is always good. 147 00:10:27,690 --> 00:10:30,060 And then let's go walk over to one of our ammo boxes. 148 00:10:30,060 --> 00:10:34,200 And as you can see, it currently displays how much ammo is left inside of the ammo box. 149 00:10:34,200 --> 00:10:38,850 So if I get rid of one bullet and then I reload my gun. 150 00:10:41,500 --> 00:10:44,380 So I have one bullet missing in my reserve ammo. 151 00:10:44,380 --> 00:10:47,920 If I go ahead and use the ammo box to refill my gun. 152 00:10:48,490 --> 00:10:54,010 As you can see, it subtracted one bullet out of my ammo box and placed that inside of the reserve ammo 153 00:10:54,010 --> 00:10:55,090 for my gun. 154 00:10:55,570 --> 00:10:56,410 Very cool. 155 00:10:59,390 --> 00:11:03,140 So again, I need 40 bullets for my reserve ammo. 156 00:11:03,140 --> 00:11:05,570 So if I go ahead and use this ammo box here. 157 00:11:06,890 --> 00:11:12,200 As you can see, it subtracted 40 bullets from my ammo box and placed those 40 bullets inside of my 158 00:11:12,200 --> 00:11:12,830 ammo reserve. 159 00:11:12,860 --> 00:11:13,790 Very cool. 160 00:11:14,680 --> 00:11:20,200 Now what I'm going to do is I'm going to duplicate one of these ammo piles and then place it over at 161 00:11:20,200 --> 00:11:21,220 my spawn. 162 00:11:22,730 --> 00:11:25,640 And then I'm going to update the attribute of current ammo. 163 00:11:25,640 --> 00:11:28,220 And I'm just going to set that equal to one. 164 00:11:28,220 --> 00:11:34,670 And we can go and playtest our game, because I want to see if it successfully refills my ammo box after 165 00:11:34,670 --> 00:11:36,050 I take all the ammo out of it. 166 00:11:36,050 --> 00:11:43,100 So let me shoot my gun and reload and let me go ahead and take this last bullet that is inside of this 167 00:11:43,100 --> 00:11:44,120 animal pile. 168 00:11:44,600 --> 00:11:46,430 And as you can see, there's no more ammo left. 169 00:11:46,430 --> 00:11:48,740 And the proximity prompt disappeared. 170 00:11:48,740 --> 00:11:54,770 So all we have to do now is wait to see how long it takes for this to refill the ammo inside of it, 171 00:11:54,770 --> 00:11:57,200 so I'll catch you when it refills. 172 00:12:00,130 --> 00:12:07,030 And just like that, our ammo pile has refilled itself and the proximity prompt has been re-enabled. 173 00:12:07,030 --> 00:12:13,480 So now if I get rid of some of the ambience out of my gun, and then I go ahead and use this ammo pile, 174 00:12:13,480 --> 00:12:15,880 hopefully, as you can see, perfect. 175 00:12:15,880 --> 00:12:16,570 It still works. 176 00:12:16,570 --> 00:12:20,050 So subtracted some ammo out of it and gave it to me in my gun. 177 00:12:20,050 --> 00:12:20,830 Very cool. 178 00:12:21,510 --> 00:12:23,550 Great job so far with this project. 179 00:12:23,550 --> 00:12:27,930 I hope it is thoroughly challenging for you and I'll see you in the next lecture.